<?php
defined('BASEPATH') OR exit('No direct script access allowed');

define('BALANCE_TYPE', 1); //余额支付类型
define('WECHAT_TYPE', 2);  //微信支付类型
define('ALIPAY_TYPE', 3);  //支付宝支付类型

/**
 * 产品
 * @author www.dingwei.cn
 */
class Pay extends MY_Controller {
	
	/**
	 * 支付模型
	 * @var ConsumerPayModel
	 */
	public $mPay;
	
	/**
	 * 银行模型
	 * @var ConsumerBankModel
	 */
	public $mBank;
	
	/**
	 * 订单模型
	 * @var ConsumerOrderModel
	 */
	public $mOrder;
	/**
	 * 活动
	 * @var MerchantPromotionModel
	 */
	public $mMerPromotion;
	/**
	 * 活动
	 * @var ConsumerPromotionModel
	 */
	public $mPromotion;
	
	//订单信息
	public $order = NULL;
	public $merchatIds = array();
	public $amount = 0;
	public function __construct(){
		parent::__construct();
		$this->loadModel('ConsumerPayModel', 'mPay', 'consumer');
	}
	
	
	/**
	 * 余额支付
	 */
	public function balance(){
		//表单验证
		$vConf = array(
				array('pay_code', '预支付订单号', 'required'),
				array('pay_passwd', '支付密码', 'required'),
		);
		$this->formDataValidation($vConf);
		
		$payCode = post('pay_code');
		$pay_passwd = post('pay_passwd');
		
		$tPayOrder = $this->mPay->getInfo($this->userId,$payCode);
		if ( ! $tPayOrder){
			$this->appReturn(0,'订单不存在');
		}
		
		$tUser = $this->mUser->getInfo($this->userId);
// 		$this->appReturn(0, md5($pay_passwd));
		if ($tUser['pay_passwd'] != setPassword($pay_passwd)){
			$this->appReturn(0,'支付密码错误');
		}
		
		if ($tPayOrder['status'] == 2){
			$this->appReturn(0,'该订单已经支付');
		}
		if($this->userBalance < $tPayOrder['amount']){
		    $this->appReturn(0, '抱歉，余额不足');
        }
		$res = $this->_successPay($payCode, BALANCE_TYPE);
		if (! $res['status']){
			$this->appReturn(0,$res['msg']);
		}
		$this->appReturn(1,$res['msg'],array('balance'=>$this->userBalance));
	}
	
	/**
	 * 充值余额
	 */
	public function recharge(){
		//表单验证
		$vConf = array(
				array('amount', '充值金额', 'required|is_natural_no_zero'),
				array('pay_type', '支付方式', 'required|in_list[2,3]'),
		);
		$this->formDataValidation($vConf);
		
		$amount = trim(post('amount'));
		$pay_type = trim(post('pay_type'));
		if ($amount > 1000){
		    $this->appReturn(0, '一次金额最多可充一千');
        }
		//生成订单
		$code = getOrderCode();
		$data = array(
				'consumer_id'=>$this->userId,
				'code'=>$code,
				'amount'=>$amount,
				'type'=>1,//充值
				'pay_type'=>$pay_type,
				'status'=>1,//未支付
				'param'=>json_encode(array()),
				'addtime'=>time(),
		);
		
		$this->db->trans_begin();
		if ( ! $this->mPay->savePayOrder($data)){
			$this->db->trans_rollback();
			$this->appReturn(0,'充值失败');
		}
		
		//获取支付宝
		$tA['alipay'] = $this->mPay->_getAliPay($code,$amount);
		
		//获取微信支付
		$tA['wxpay'] = $this->mPay->_getWxPay($code,$amount);
		
		$this->db->trans_commit();
		$this->appReturn(1,'',$tA);
	}

	////////////////////////////////////////////////////
	//+-----------------------------------------------
	//|	回调
	//+----------------------------------------------
	/**
	 * 支付宝回调
	 */
	public function zfbCallback(){
		
		//$this->db->insert('pay_callback',array('type'=>'alipay','content'=>json_encode($_REQUEST)));
		
		$trade_status = post('trade_status');
		if ( ! isset($trade_status)){
			exit('fail');
		}
		
		//保存参数
		$this->mPay->savePayOrder(array('callback'=>json_encode($_POST)),post('out_trade_no'));
		
		$res = $this->mPay->_aliCallback();
		if ($res){
			$tRes = $this->_successPay(post('out_trade_no'),ALIPAY_TYPE);
			if(!$tRes['status']){
				exit('fail');
			}
		}else{
			exit('fail');
		}
		exit('success');
	}
	
	/**
	 * 微信支付回调
	 */
	public function wxCallback(){
		
		include APPPATH.'libraries/WxpayAPI/lib/WxPay.Api.php';
		$postStr = file_get_contents('php://input');
		$xmlObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$str = (array)$xmlObj;//如果要数组 强制类型状态
		
		
		//$this->db->insert('pay_callback',array('type'=>'wxpay','content'=>json_encode($str)));
// 		$xmlObj = '{"appid":"wx3fc9382b8dd0c862","bank_type":"CFT","cash_fee":"1","fee_type":"CNY","is_subscribe":"N","mch_id":"1483121982","nonce_str":"ma7jetoi5sdjxebkvzylq6la7yue7ag3","openid":"ok_X2wyZGsBcKUiMMxs7RMQgVAik","out_trade_no":"20171214045902847661","result_code":"SUCCESS","return_code":"SUCCESS","sign":"2CDE99F56BEE183EA2791C2766BF2B11","time_end":"20170803172231","total_fee":"1","trade_type":"APP","transaction_id":"4000642001201708034264916205"}';
// 		$xmlObj = json_decode($xmlObj);
		
		//保存参数
		$this->mPay->savePayOrder(array('callback'=>json_encode($str)),$xmlObj->out_trade_no);
		
		$pay = new WxPayApi();
		if( ! empty($xmlObj->return_code) && $xmlObj->return_code == 'SUCCESS'){
			$input = new WxPayOrderQuery();
			$input->SetOut_trade_no($xmlObj->out_trade_no);
			$result = WxPayApi::orderQuery($input);
			if( ! empty($result) && $result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS'){
				$tRes = $this->_successPay($xmlObj->out_trade_no, WECHAT_TYPE);
				if( ! $tRes['status']){
					exit($tRes['msg']);
				}
			}
		}
		
		$obj = new WxPayNotifyReply();
		$obj->SetReturn_code('SUCCESS');
		$obj->SetReturn_msg('OK');
		exit($obj->ToXml());
	}
	
	////////////////////////////////////////////////////
	//+-----------------------------------------------
	//|	支付后续处理
	//+----------------------------------------------
	
	/**
	 * 成功支付后
	 * @param unknown $order
	 * @param unknown $payType	1-余额支付  2.微信支付 3-支付宝
	 * @return number[]|string[]
	 */
	private function _successPay($payCode,$payType){
        $this->loadModel('ConsumerMessageModel', 'mMsg','consumer');

		$order = $this->mPay->getInfo(0,$payCode);
		
		if( ! isset($order['code'])){
			return array('status'=>0,'msg'=>'预支付订单号不存在');
		}
		
		if( ! isset($order['type'])){
			return array('status'=>0,'msg'=>'订单类型错误');
		}
		
		$payCode	= $order['code'];		//订单号
		$type		= $order['type']; //类型
		$this->amount = $amount 	= $order['amount'];  //用户
		$consumerId = $order['consumer_id'];  //用户
		$param 		= $order['param'];  //参数 下单和回调对应
		
		/**
		 * 预支付订单状态
		 * @var array $data
		 */
		$this->db->trans_begin();//事务

		$data = array(
				'status'=>2,
				'paytime'=>time(), //更新支付时间
				'pay_type'=>$payType, //支付方式
		);
		$query = $this->mPay->savePayOrder($data,$payCode);
		if( ! $query){
			//订单状态修改失败
			$this->db->trans_rollback();
			return array('status'=>0,'msg'=>'支付失败');
		}
		
		/**
		 * 修改订单状态
		 * @var array $tPay
		 */
		if( ! in_array($type,array(1,2))){
			$this->db->trans_rollback();
			return array('status'=>0,'msg'=>'订单类型错误');
		}
		
		//加载模型
		$this->loadModel('ConsumerUserModel', 'mConsumer','consumer');
		
		//获取用户信息
		$tComsumer = $this->mConsumer->getInfo($consumerId);
		if ( ! $tComsumer){
			$this->db->trans_rollback();
			return array('status'=>0,'msg'=>'用户不存在');
		}
		
		/**
		 * 充值
		 */
		if($type == 1){
		
			/**
			 * 增加用户余额
			 */
			//修改用户余额
			if ( ! $this->mConsumer->save(array('balance'=>$tComsumer['balance'] + $amount),$consumerId)){
				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'充值失败');
			}
			
			/**
			 * 余额变更记录
			 */
			//余额变更记录
			$Buy = array(
					'consumer_id'=>$consumerId,
					'type'=>1,
					'pay_type'=>$payType,
					'amount'=>$amount,
					'param'=>json_encode(array()),
					'status'=>2,
					'addtime'=>time(),
					'confirm_time'=>time(),
			);
			if ( ! $this->mConsumer->saveBuyLog($Buy)){
				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'充值失败');
			}
			
			/**
			 * 给用户发送消息
			 */

			$Msg = array(
					'consumer_id'=>$consumerId,
					'type'=>2,
					'title'=>'充值到账消息',
					'content'=>'您于'.date('Y/m/d H:i:s').'充值'.$amount.'元!现已到账,请注意查收,订单支付编号为:'.$payCode,
					'addtime'=>time(),
			);
			if ( ! $this->mMsg->save($Msg)){
				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'充值失败');
			}
            $this->db->trans_commit();
		
		}elseif ($type == 2) {
			/**
			 * 支付
			 */
			$this->loadModel('ConsumerOrderModel', 'mOrder','consumer');
            $this->loadModel('MerchantUserModel', 'mMerchant','merchant');
            $this->loadModel('MerchantMessageModel', 'mMerMsg','merchant');
            $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
			/**
			 * 生成核销码
			 */
			$tOrder = $this->mOrder->getInfoByPayCode($payCode);
			$this->order = $tOrder;//设置订单
			
			$merchantIds = array();
			$dlnJpush = array();
			$tMerOrder = array();
			$orderIds = array();

            foreach ($tOrder as $k=>$v){
                if ( ! in_array($v['merchant_id'], $this->merchatIds)){
                    $this->merchatIds[] = $v['merchant_id'];
                }
                if ( ! in_array($v['merchant_id'], $merchantIds)){
                    $merchantIds = $v['merchant_id'];
                }
            }
            //商家信息

            $tMerchant = $this->mMerchant->getMiniInfoByIds($merchantIds);
            $tBusiness = $this->mMerchant->getBusinessMerchant($merchantIds);
			$title = '您有一条新订单';//商家消息标题
			foreach ($tOrder as $k=>$v){//单个订单数据
                if ( ! in_array($v['id'], $orderIds)){
                    $orderIds = $v['id'];
                }
                if($v['status'] != 1){
                    $this->db->trans_rollback();
                    return array('status'=>0,'msg'=>'支付失败,请勿重复提交支付');
                }
				//订单变更数据
				$oData = [];
                $oData['pay_type'] = $payType;
                $oData['pay_time'] = time();
                $oData['wx_type'] = $payType == 2 ? 1 : 0; // 微信支付来源  1 app微信支付   2 公众号微信支付
                $merchantTakeTime = microtime(true);

                //根据店铺类型。来完成不同的状态
                if ($v['expense_type'] == 1){
                    //送货上门
                    $oData['status'] = 2;

                    if(isset($tMerchant[$v['merchant_id']]) && $tMerchant[$v['merchant_id']]['auto_take'] == 2 && isset($tBusiness[$v['merchant_id']])){
                        //营业时间段内 自动接单
                        $oData['status'] = 3;
                    }

                }elseif ($v['expense_type'] == 2){
                    //到店
                    $oData['status'] = 3;//待核销
                    $tempCode =  makeCode($v['id']);//核销码
                    $oData['verification_code'] = $tempCode;
                    $oData['verification_bar_code'] = makeCodeImg($v['id'], $tempCode,'shangmeng#');
                }
                if($oData['status'] == 3){
                    $oData['merchant_take_time'] = $merchantTakeTime;
                    $oData['print_status'] = 1;
                }
                if ( ! $this->mOrder->save($oData,0,$v['id'])){
                    $this->db->trans_rollback();
                    return array('status'=>0,'msg'=>'支付失败[ERROR:001]');
                }

                /**
                 * 余额变更记录
                 */
                if ($payType == 1){
                    //余额
                    //修改用户余额
                    if ( ! $this->mConsumer->save(array('balance'=>$tComsumer['balance'] - $amount),$consumerId)){
                        $this->db->trans_rollback();
                        return array('status'=>0,'msg'=>'支付失败');
                    }
                }
                $this->db->trans_commit();

                //统计订单编号
                if($oData['status'] == 3){
                    //赠送代金券
                    $this->GiveCoupon($payCode);

                    $count = $this->mOrder->getMerchantOrderCount($v['merchant_id'], $merchantTakeTime);
                    if ( ! $this->mOrder->save(array('merchant_day_index' => $count),0,$v['id'])){
//                    $this->db->trans_rollback();
                        return array('status'=>0,'msg'=>'支付失败[ERROR:001]');
                    }
                    //生成配送单
                    if($v['shipping_type'] == 2 && $v['expense_type'] == 1){
                        //配送员接单过期时间
                        $configDln = $this->mSetting->get('rob_expect_time,deliveryman_range', 'deliveryman');
                        $rob_expect_time = $configDln['rob_expect_time'];
                        //预计送达时间 = 预计送达时间  - 配送员过期时间 - 商家接单过期时间
                        $timediff = $v['expect_arrive_time'] - time();
                        $minute = floor($timediff % 86400 / 60);
                        $tTime = (int)getCustomConfig('expect_take_time') + $rob_expect_time;//预计送达时间
                        $expect_arrive_time = $minute - $tTime;
                        //订单信息
                        $info = $this->mOrder->getInfoForDeliveryman($v['id']);
                        //商品信息
                        $productList = $this->mOrder->getOrderProductList($v['id']);
                        $product = array();
                        foreach ($productList as $pro){
                            $product[] = array(
                                'name' => $pro['name'].$pro['property_new'],
                                'quantity' => $pro['quantity']
                            );
                        }
                        //生成运单
                        $this->loadModel('DlnOrderModel', 'mDlnOrder', 'deliveryman');
                        $distance = getDistance($info['shop_latitude'], $info['shop_longitude'], $info['latitude'], $info['longitude']);
                        $result = $this->mDlnOrder->add($info['merchant_id'], $info['consumer_id'], 2, '#'.$count, $v['id'], $info['province'],
                            $info['city'], $info['county'], $info['shop_longitude'], $info['shop_latitude'], $info['merchant'],
                            $info['shop_address'], $info['store_tel'], $info['contacts'], $info['mobile'], $info['address'],
                            $info['longitude'], $info['latitude'], $product, $info['remark'], $expect_arrive_time,
                            $info['deliveryman_income'], $weight = 0, $distance, $info['expect_arrive_time']);
                        if ($result['status'] != 1) {
//                        $this->db->trans_rollback();
                            return array('status'=>0,'msg'=>'运单生成失败[ERROR:002]');
                        }
                        //平台配送 通知配送员
                        $this->loadModel('DlnUserModel', 'mDlnUser', 'deliveryman');

                        $distance = $configDln['deliveryman_range'];//配送员接单范围
                        $distance = $distance>0 ? round($distance/1000,2):0;
                        $dlnData = $this->mDlnUser->getDlnUser($info['latitude'], $info['longitude'], $distance);
                        if($dlnData){
                            $dlnJpush = array_column($dlnData, 'jpush_code');
                            deliverymanJpush('新订单提醒','来新订单啦~赶快去抢单吧', 1, $dlnJpush, 0, 'new_order.mp3', 1);
                        }
                    }
                }


				//给商家的推送消息
				$merMsg[] = array(
						'merchant_id'=>$v['merchant_id'],
						'type'=>3,
						'title'=>'新订单提醒',
						'content'=>'请及时发货',
						'order_id'=>$v['id'],
						'addtime'=>time(),
				);
				
				//给用户发消息
				$Msg[] = array(
						'consumer_id'=>$consumerId,
						'type'=>3,
						'title'=>'订单已支付',
						'content'=>'等待商家发货',
						'order_id'=>$v['id'],
						'addtime'=>time(),
				);
				

				$tMerOrder[$v['merchant_id']] = $v['id'];

            }


			//余额变更记录
			$Buy = array(
					'consumer_id'=>$consumerId,
					'type'=>2,
					'pay_type'=>$payType,
					'amount'=>'-'.$amount,
					'param'=>json_encode($orderIds),
					'status'=>2,
					'addtime'=>time(),
					'confirm_time'=>time(),
			);
			if ( ! $this->mConsumer->saveBuyLog($Buy)){
//				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'充值失败');
			}
			
			/**
			 * 写订单操作记录
			 * @var array $tBOrder
			 */
			$tBOrder = $this->mOrder->getInfoByCodes(json_decode($param,true));
            $note = '支付成功';
			foreach ($tBOrder as $k=>$v){
				$this->mOrder->saveOrderLog($v['id'], '已付款', $note, $this->port, $v['consumer_id']);
			}
			
			/**
			 * 给用户发送消息
			 */

			if ( ! $this->mMsg->saveBatch($Msg)){
//				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'支付失败');
			}
			
			
			/**
			 * 给商家发送消息 新订单提示
			 */

			//批量保存商家消息
			if ( ! $this->mMerMsg->saveBatch($merMsg)){
//				$this->db->trans_rollback();
				return array('status'=>0,'msg'=>'支付失败');
			}
			
			//推送

			$tJpushCode = array();
			foreach ($tMerchant as $k=>$v){
				//$tJpushCode[] = $v['jpush_code'];
				//批量通知商家
				MerchantJpush($title,$title,1,[$v['jpush_code']],3,$tMerOrder[$v['id']] , 0,'push_ordering.mp3', 1);
			}

			// 购买送积分
//            $this->integralCoefficient($tComsumer['integral'], $consumerId);
//            $this->experienceValueCoefficient($tComsumer['integral'], $consumerId); // 购买获得经验奖励
		}
		
//		$this->db->trans_commit();
		return array('status'=>1,'msg'=>'支付成功');
	}
	
	/**
	 * 下单即可送代金劵
	 */
	private function GiveCoupon($payCode){
		if ( ! $payCode){
			return false;
		}
		
		if ( ! $this->order){
			//为空
			return false;
		}
		
		if ( ! $this->merchatIds){
			//订单商家为空
			return false;
		}
		
		$this->loadModel('ConsumerPromotionModel', 'mPromotion','consumer');
		$this->loadModel('MerchantPromotionModel', 'mMerPromotion','merchant');
		
		$tCoupon = $this->mMerPromotion->getCouponListByMerchantIds($this->merchatIds);
		if ( ! $tCoupon){
			return true;
		}
		
		$tGiveCoupon = array();
		foreach ($this->order as $k=>$v){
			if ( ! isset($tCoupon[$v['merchant_id']])){
				//不存在
				continue;
			}
			
			if ($v['reality_amount'] >= $tCoupon[$v['merchant_id']]['receive']){
				//实际支付金额 > 代金劵领取条件
				//装入代金劵批量
//				$tGiveCoupon[] = array(
//						'merchant_id'	=> $v['merchant_id'],
//						'consumer_id'	=> $v['consumer_id'],
//						'order_id'		=> $v['id'],
//						'money'			=> $tCoupon[$v['merchant_id']]['money'],
//						'valid_hour'	=> $tCoupon[$v['merchant_id']]['valid_hour'],
//						'send_time'		=> time(),
//						'status'		=> 1,
//						'used_time'		=> 0,
//						'condition'		=> $tCoupon[$v['merchant_id']]['condition'],
//						'use_end_time'	=> time() + $tCoupon[$v['merchant_id']]['valid_hour'] * 3600,
//				);
			}
		}
		
//		if ($tGiveCoupon){
//			if ( ! $this->mPromotion->saveBatchCouponLog($tGiveCoupon)){
//				return false;
//			}
//		}
		
		return true;
	}
	
	/**
	 * 申请提现
	 */
	public function deposit(){
		//表单验证
		$vConf = array(
				array('bank_id', '银行卡id', 'required|is_natural_no_zero'),
				array('amount', '提现金额', 'required|is_natural_no_zero'),
				array('pay_type', '提现类型', 'required|in_list[3,4]'),
				array('pay_passwd', '支付密码', 'required'),
		);
		$this->formDataValidation($vConf);
		
		$bankId = post('bank_id');
		$amount = post('amount');
		$pay_type = post('pay_type');
		$pay_passwd = post('pay_passwd');
		
		//加载模型
		$this->loadModel('ConsumerBankModel', 'mBank','consumer');
		$this->loadModel('ConsumerUserModel', 'mConsumer','consumer');
		
		//验证支付密码
		$tUser = $this->mUser->getInfo($this->userId);
		if ( $tUser['pay_passwd'] !== setPassword($pay_passwd) ){
			$this->appReturn(0,'支付密码错误');
		}
		
		//获取信息
		$tBank = $this->mBank->getInfo($bankId,$this->userId);
		if ( ! $tBank){
			$this->appReturn(0,'银行卡不存在');
		}
		/**
		 * 余额变更
		 */
		
		if ($this->userBalance < $amount){
			$this->db->trans_rollback();
			$this->appReturn(0,'余额不足');
		}
		
		$this->db->trans_begin();//事务
		$changeBalance = $this->userBalance - $amount;
		if ( ! $this->mUser->save(array('balance'=>$changeBalance),$this->userId)){
			$this->db->trans_rollback();
			$this->appReturn(0,'银行卡不存在');
		}
		
		/**
		 * 余额变更记录
		 */
		//余额变更记录
		$Buy = array(
				'consumer_id'=>$this->userId,
				'type'=>4,//提现
				'pay_type'=>$pay_type,//提现到银行卡
				'amount'=>'-'.$amount,
				'param'=>json_encode($tBank),
				'status'=>3,
				'addtime'=>time(),
		);
		if ( ! $this->mConsumer->saveBuyLog($Buy)){
			$this->db->trans_rollback();
			$this->appReturn(0,'申请失败');
		}
		
		$this->db->trans_commit();
		$this->appReturn(1,'申请成功',array('balance'=>$changeBalance));
	}

    /**
     * 购买送积分
     * @param $integral         int         原积分
     * @param $consumerId       int         用户id
     */
    public function integralCoefficient($integral, $consumerId){
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
        $this->loadModel('ConsumerMallModel', 'mMall');
        $number = $this->mSetting->get('integral_coefficient'); // 注册赠送积分数量
        if ($number > 0) {
            $ig = floor($this->amount * ($number/100));
            if ($ig == 0){
                return;
            }
            $integral = $this->mUser->save(array('integral' => $integral + $ig), $consumerId);
            if (!$integral) {
                $this->db->trans_rollback();
                $this->ajaxReturn(0, '支付失败');
            }
            // 添加积分记录
            $integralLogData = array(
                'consumer_id' => $consumerId,
                'type' => 6,
                'integral' => $ig,
                'status' => 2,
                'addtime' => time(),
            );
            $integralLog = $this->mMall->saveLog($integralLogData);
            if (!$integralLog) {
                $this->db->trans_rollback();
                $this->ajaxReturn(0, '支付失败');
            }
        }
    }

    /**
     * 在线购买获得经验奖励系数
     * @param $experienceValue   int         原经验数
     * @param $consumerId       int         用户id
     */
    public function experienceValueCoefficient($experienceValue, $consumerId){
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
        $this->loadModel('ConsumerExperienceValueModel', 'mExperience');
        $number = $this->mSetting->get('experience_value_coefficient'); // 在线购买获得经验奖励系数
        if ($number > 0) {
            $ig = floor($this->amount * ($number/100));
            if ($ig == 0){
                return;
            }
            $exp = $this->mUser->save(array('experience_value' => $experienceValue + $ig), $consumerId);
            if (!$exp) {
                $this->db->trans_rollback();
                $this->ajaxReturn(0, '支付失败');
            }
            // 添加积分记录
            $logData = array(
                'consumer_id' => $consumerId,
                'type' => 5,
                'experience_value' => $ig,
                'addtime' => time(),
            );
            $log = $this->mExperience->save($logData);
            if (!$log) {
                $this->db->trans_rollback();
                $this->ajaxReturn(0, '支付失败');
            }
        }
    }
}